﻿// MIT 许可证
// 版权 © 2023-present https://github.com/dymproject/purest-admin作者 以及贡献者
// 作者或版权持有人都不对任何索赔、损害或其他责任负责，无论这些追责来自合同、侵权或其它行为中，
// 还是产生于、源于或有关于本软件以及本软件的使用或其它处置。

using Purest.Multiplex.Contracts.IAdminUser;
using Purest.SqlSugar.Entity;

namespace Purest.Multiplex.AdminUser;
/// <summary>
/// 当前用户
/// </summary>
public class CurrentUser(ISqlSugarClient db, IHttpContextAccessor httpContextAccessor) : ICurrentMerchant, ITransientDependency
{
    /// <summary>
    /// db
    /// </summary>
    private readonly ISqlSugarClient _db = db;

    /// <summary>
    /// HttpContext 访问器
    /// </summary>
    private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;

    /// <summary>
    /// 获取用户 Id
    /// </summary>
    public long Id
    {
        get => long.Parse(_httpContextAccessor.HttpContext?.User.FindFirst("userid")?.Value ?? throw Oops.Bah("令牌超时，请重新登录！"));
    }

    public UserEntity Self
    {
        get => _db.Queryable<UserEntity>().First(x => x.Id == Id) ?? throw Oops.Bah("用户不存在！");
    }
}
